package craky.componentc;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.TableModel;

import com.abigdreamer.swing.component.JCButton;
import com.abigdreamer.swing.component.UIResourceManager;

import craky.component.JImagePane;

public class JCScrollTable extends JCScrollPane implements ActionListener {
	private static final long serialVersionUID = 4276053919432949896L;
	private static final Color DISABLED_BG = UIResourceManager.getColor("TextDisabledBackground");
	public static final String COMMAND_SHOW_MENU = "ShowMenu";
	private Border border;
	private Border disabledBorder;
	private Color background;
	private JCTable table;
	private JCButton btnUpperRight;

	public JCScrollTable(JCTable table) {
		setViewportView(this.table = table);
		init();
	}

	public JCScrollTable() {
		this(new JCTable());
	}

	public JCScrollTable(TableModel dm) {
		this(new JCTable(dm));
	}

	public JCScrollTable(Object[][] rowData, Object[] columnNames) {
		this(new JCTable(rowData, columnNames));
	}

	public JCScrollTable(Vector<?> rowData, Vector<?> columnNames) {
		this(new JCTable(rowData, columnNames));
	}

	private void init() {
		setBorder(new LineBorder(new Color(84, 165, 213)));
		setDisabledBorder(new LineBorder(new Color(84, 165, 213, 128)));
		setBackground(UIResourceManager.getWhiteColor());
		setCorner("UPPER_RIGHT_CORNER", createUpperRightCorner());
		setHeaderDisabledForeground(new Color(123, 123, 122));
		this.table.setBorder(new EmptyBorder(0, 0, 0, 0));
		this.table.setDisabledBorder(this.table.getBorder());
		this.table.setVisibleInsets(0, 0, 0, 0);
		this.table.setAlpha(0.0F);
		this.table.setBackground(UIResourceManager.getEmptyColor());
		this.table.getTableHeader().setBackground(UIResourceManager.getEmptyColor());
	}

	protected void initHeader() {
	}

	private JComponent createUpperRightCorner() {
		Image defaultImage = UIResourceManager.getImage("ScrollTableShowMenuButtonImage");
		JImagePane pane = new JImagePane();
		this.btnUpperRight = new JCButton();
		pane.setImageOnly(true);
		pane.setBorder(new EmptyBorder(0, 0, 1, 0));
		pane.setLayout(new BorderLayout());
		pane.setFilledBorderArea(false);
		pane.setMode("Scaled");
		pane.setImage(defaultImage);
		pane.setAlpha(0.5F);
		this.btnUpperRight.setImage(defaultImage);
		this.btnUpperRight.setRolloverImage(UIResourceManager.getImage("ScrollTableShowMenuButtonRolloverImage"));
		this.btnUpperRight.setPressedImage(UIResourceManager.getImage("ScrollTableShowMenuButtonPressedImage"));
		this.btnUpperRight.setDisabledImage(this.btnUpperRight.getImage());
		this.btnUpperRight.setFocusable(false);
		this.btnUpperRight.setActionCommand("ShowMenu");
		this.btnUpperRight.addActionListener(this);
		pane.add(this.btnUpperRight);
		return pane;
	}

	public Border getDisabledBorder() {
		return this.disabledBorder;
	}

	public void setDisabledBorder(Border disabledBorder) {
		this.disabledBorder = disabledBorder;

		if (!isEnabled()) {
			super.setBorder(disabledBorder);
		}
	}

	public void setBorder(Border border) {
		this.border = border;
		super.setBorder(border);
	}

	public Color getDisabledForeground() {
		return this.table.getDisabledForeground();
	}

	public void setDisabledForeground(Color disabledForeground) {
		this.table.setDisabledForeground(disabledForeground);
	}

	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		super.setBorder(enabled ? this.border : this.disabledBorder);
		super.setBackground(enabled ? this.background : DISABLED_BG);
		this.btnUpperRight.setVisible(enabled);
	}

	public void setBackground(Color background) {
		this.background = background;
		super.setBackground(background);
	}

	public boolean isColumnControlEnabled() {
		return this.btnUpperRight.isEnabled();
	}

	public void setColumnControlEnabled(boolean enabled) {
		this.btnUpperRight.setEnabled(enabled);
	}

	public JCTable getTable() {
		return this.table;
	}

	@Deprecated
	public JLabel getHeaderLabel() {
		return null;
	}

	@Deprecated
	public HeaderPane getHeader() {
		return null;
	}

	@Deprecated
	public String getHeaderText() {
		return null;
	}

	@Deprecated
	public void setHeaderText(String text) {
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("ShowMenu")) {
			JPopupMenu menu = this.table.getColumnControlMenu();

			if ((menu != null) && (menu.getComponentCount() > 0)) {
				Dimension buttonSize = this.btnUpperRight.getSize();
				menu.show(this.btnUpperRight, buttonSize.width - menu.getPreferredSize().width, buttonSize.height);
			}
		}
	}

	public void adjustmentValueChanged(AdjustmentEvent e) {
	}
}